package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetMealService setMealService;

    /**
     * 图片上传
     * @param imgFile 图片
     * @return        上传结果flag
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) {

        //获取到file的名字,然后通过uuid来设置
        String uuid = UUID.randomUUID().toString();
        String filename = imgFile.getOriginalFilename();
        int lastIndexOf = filename.lastIndexOf(".");
        //获取文件后缀
        String newFileLastName = filename.substring(lastIndexOf - 1);
        String fileName=uuid+newFileLastName;
        //组合成新的文件名字,同时回传
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
            //文件上传成功后,把数据放在redis里面

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }}

    /**
     * 添加套餐和套餐检查组关系
     * @param checkGroupIds 套餐和检查组关系表
     * @param setmeal       套餐
     * @return              添加结果flag
     */
    @RequestMapping("/add")
    public Result add(@RequestParam("checkGroupIds") Integer[] checkGroupIds, @RequestBody Setmeal setmeal) {
        try {
            setMealService.addSetMealAndCheckGroupIds(checkGroupIds, setmeal);
            String url = setmeal.getImg();
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,url);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 主要套餐内容查找
     * @param pageBean  每页条数,页码,查询条件
     * @return          setMeal结果集
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean pageBean) {
        try {
            return setMealService.findByPage(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    /**
     * 通过id查询套餐
     * @param id 套餐id
     * @return   套餐
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal=setMealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 套餐修改
     * @param checkGroupIds 套餐和检查组关系表
     * @param setmeal       套餐(包含套餐id)
     * @return              修改结果flag
     */
    @RequestMapping("/edit")
    public Result edit(@RequestParam("checkGroupIds") Integer[] checkGroupIds,@RequestBody Setmeal setmeal){
        try {
            setMealService.edit(checkGroupIds,setmeal);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 通过套餐id查询其包含的检查组
     * @param id 套餐id
     * @return   检查组id集
     */
    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id){
        try {
            List<Integer> checkgroupIds=setMealService.findCheckGroupById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 通过套餐id删除
     * @param id    套餐id
     * @return      删除结果flag
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setMealService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
}
